import java.util.*;

public class Customer extends User{
    public String ID;
    public String level = "铜牌客户";
    boolean lockFlag = false;
    public String registerTime;
    private int costTotal = 0;
    private String phoneNumber;
    public String buyHistory = "";
    private String mailbox;
    public GoodsSet goodsSet;
    HashMap<Good, Integer> shoppingTrolley = new HashMap<Good, Integer>();

    Customer(String account, String password, GoodsSet goodsSet){
        setAccount(account);
        setPassword(password);
        setGoodsSet(goodsSet);
    }
    public void setGoodsSet(GoodsSet goodsSet){
        this.goodsSet = goodsSet;
    }
    public void showHistory(){
        Scanner scanner = new Scanner(System.in);
        System.out.println(buyHistory);
        System.out.println("输入任意字符返回");
        scanner.next();
    }
    public void setID(String id){
        ID = id;
    }
    public void forgetPassword(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入邮箱");

        while(true){
            String mailbax = scanner.next();
            if(mailbax.equals(getMailbox())){
                System.out.println("您的新密码为：");
                String newPassword = randPassword();
                System.out.println(newPassword);
                setPassword(newPassword);
                System.out.println("输入任意字符继续");
                scanner.next();
                return;
            }
            System.out.println("输入邮箱错误，请重新输入");
        }
    }
    public String randPassword(){
        Random random = new Random();
        String str = "";
        StringBuilder sb = new StringBuilder(str);

        for(int i = 0; i < 8; i++){
            char s = (char) random.nextInt(128);
            sb.append(s);
        }
        String result = sb.toString();
        if(checkPassword(result)){
            return result;
        }
        return randPassword();
    }
    public int getCostTotal(){return costTotal;}
    public String getPhoneNumber(){return phoneNumber;}
    public String getMailbox(){return mailbox;}
    public void setMailbox(String mailbox){
        this.mailbox = mailbox;
    }
    public void setPhoneNumber(String phoneNumber){
        this.phoneNumber = phoneNumber;
    }


    @Override
    public void changePassword() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入原密码：");
        if(!input.next().equals(getPassword())){
            System.out.println("原密码错误");
            System.out.println("输入任意字符返回上一级");
            input.next();
            return;
        }
        while (true){
            System.out.println("请输入新密码");
            String newPassword = input.next();
            if(checkPassword(newPassword)){
                setPassword(newPassword);
                System.out.println("修改成功！！");
                return;
            }
            System.out.println("密码长度大于8个字符，必须是大小写字母、数字和标点符号的组合,请重新输入");
        }
    }

    private boolean checkPassword(String password){
        if(password.length() < 8)return false;
        boolean hasUpperCase = false;
        boolean hasLowerCase = false;
        boolean hasDigit = false;
        boolean hasSpecialChar = false;

        for (int i = 0; i < password.length(); i++) {
            char ch = password.charAt(i);
            if (Character.isUpperCase(ch)) {
                hasUpperCase = true;
            } else if (Character.isLowerCase(ch)) {
                hasLowerCase = true;
            } else if (Character.isDigit(ch)) {
                hasDigit = true;
            } else{
                hasSpecialChar = true;
            }
        }
        return hasUpperCase && hasLowerCase && hasDigit && hasSpecialChar;
    }
    @Override
    public void passwordManagement() {
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("--------------------------------------------------------------------------------------------");
            System.out.println("1--修改自身密码");
            System.out.println("2--忘记密码");
            System.out.println("3--返回上一级");
            int choose = 0;
            try {
                choose = scanner.nextInt();
            }catch (Exception e){
                System.out.println("输入不合法，请重新输入。");
                while (scanner.hasNext()) {
                    if (scanner.hasNextLine()) {
                        scanner.nextLine();
                        break;
                    } else {
                        scanner.next();
                    }
                }
                passwordManagement();
                return;
            }
            switch (choose){
                case 1:
                    changePassword();
                    break;
                case 2:
                    forgetPassword();
                    break;
                case 3:
                    return;
                default:
                    System.out.println("输入不合法，请重新输入。");
                    break;
            }
        }
    }


    public void resetPassword(){
        setPassword("12345abcde");
    }
    public void shoppingManagement(){
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("-------------------------------------------------------------------------------------------");
            System.out.println("1--将商品加入购物车");
            System.out.println("2--将商品从购物车中移除");
            System.out.println("3--修改购物车中的商品");
            System.out.println("4--结账");
            System.out.println("5--查看购物历史");
            System.out.println("6--返回上一级");
            int choose = 0;
            try {
                choose = scanner.nextInt();
            }catch (Exception e){
                System.out.println("输入不合法，请重新输入。");
                while (scanner.hasNext()) {
                    if (scanner.hasNextLine()) {
                        scanner.nextLine();
                        break;
                    } else {
                        scanner.next();
                    }
                }
                shoppingManagement();
                return;
            }
            switch (choose){
                case 1:
                    addToTrolley();
                    break;
                case 2:
                    moveFrmTrolley();
                    break;
                case 3:
                    changeTrolley();
                    break;
                case 4:
                    pay();
                    break;
                case 5:
                    showHistory();
                    break;
                case 6:
                    return;
                default:
                    System.out.println("输入不合法，请重新输入。");
                    break;
            }
        }
    }

    public void pay(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入日期：");
        String date = scanner.next();
        int totalMoney = 0;
        System.out.println("您即将要购买购物车中的全部物品，总共"+totalMoney+"元，请选择支付方式");
        System.out.println("1--微信支付\n2--支付宝支付\n3--银行卡支付");
        int choose = 0;
        try {
            choose = scanner.nextInt();
        }catch (Exception e){
            System.out.println("输入不合法，请重新输入。");
            while (scanner.hasNext()) {
                if (scanner.hasNextLine()) {
                    scanner.nextLine();
                    break;
                } else {
                    scanner.next();
                }
            }
            pay();
            return;
        }
        switch (choose){
            case 1:
                System.out.println("正在使用微信支付。。。");
                for(Good key : shoppingTrolley.keySet()){
                    totalMoney += key.getPrice() * shoppingTrolley.get(key);
                    buyHistory = buyHistory+date+" 购买了"+shoppingTrolley.get(key)+"件"+key.getName()+"总共"+key.getPrice() * shoppingTrolley.get(key)+"元\n";
                    key.setCount(key.getCount()-shoppingTrolley.get(key));
                }
                shoppingTrolley.clear();
                costTotal += totalMoney;
                System.out.println("输入任意字符返回");
                scanner.next();
                break;
            case 2:
                System.out.println("正在使用支付宝支付。。。");
                for(Good key : shoppingTrolley.keySet()){
                    totalMoney += key.getPrice() * shoppingTrolley.get(key);
                    buyHistory = buyHistory+date+" 购买了"+shoppingTrolley.get(key)+"件"+key.getName()+"总共"+key.getPrice() * shoppingTrolley.get(key)+"元\n";
                    key.setCount(key.getCount()-shoppingTrolley.get(key));
                }
                shoppingTrolley.clear();
                costTotal += totalMoney;
                System.out.println("输入任意字符返回");
                scanner.next();
                break;
            case 3:
                System.out.println("正在使用银行卡支付。。。");
                for(Good key : shoppingTrolley.keySet()){
                    totalMoney += key.getPrice() * shoppingTrolley.get(key);
                    buyHistory = buyHistory+date+" 购买了"+shoppingTrolley.get(key)+"件"+key.getName()+"总共"+key.getPrice() * shoppingTrolley.get(key)+"元\n";
                    key.setCount(key.getCount()-shoppingTrolley.get(key));
                }
                shoppingTrolley.clear();
                costTotal += totalMoney;
                System.out.println("输入任意字符返回");
                scanner.next();
                break;
            default:
                System.out.println("输入错误，请重新输入。");
                pay();
                break;
        }
    }

    public void changeTrolley(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择要修改的商品(输入需要修改商品名称)");
        for(Good key : shoppingTrolley.keySet()){
            key.showLittle();
        }
        String name = scanner.next();
        Good good = null;
        for(Good go : goodsSet.goodsList){
            if(name.equals(go.getName())){
                good = go;
            }
        }
        System.out.println("输入修改后的商品数量：");
        int num = 0;
        try {
            num = scanner.nextInt();
        }catch (Exception e){
            System.out.println("输入不合法，请重新输入。");
            while (scanner.hasNext()) {
                if (scanner.hasNextLine()) {
                    scanner.nextLine();
                    break;
                } else {
                    scanner.next();
                }
            }
            changeTrolley();
            return;
        }
        if(num < 0){
            System.out.println("输入不可以是负数。。。。。请重新修改");
            changeTrolley();
            return;
        }
        if(num > good.getCount()){
            System.out.println("商品数量不足！");
            System.out.println("输入任意字符继续");
            scanner.next();
            return;
        }
        shoppingTrolley.put(good, num);
        System.out.println("修改成功！！");
    }

    public void moveFrmTrolley(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请选择要移除的商品(输入需要删除商品名称)");
        for(Good key : shoppingTrolley.keySet()){
            key.showLittle();
            System.out.println("   在购物车中的数量："+shoppingTrolley.get(key));
        }
        String name = scanner.next();
        Good good = null;
        for(Good go : goodsSet.goodsList){
            if(name.equals(go.getName())){
                good = go;
            }
        }
        shoppingTrolley.remove(good);
    }
    public void addToTrolley(){
        Scanner scanner = new Scanner(System.in);
        for (Good good : goodsSet.goodsList){
            good.showLittle();
            System.out.println();
        }
        System.out.println("输入商品编号和数量将指定的商品加入到购物车");
        System.out.println("输入商品编号");
        String number = scanner.next();
        Good good = null;
        for(Good go : goodsSet.goodsList){
            if(go.getNumber().equals(number)){
                good = go;
            }
        }
        if(good==null){
            System.out.println("不存在该商品，输入任意字符继续");
            scanner.next();
            return;
        }
        System.out.println("输入商品数量");
        int cnt = 0;
        try {
            cnt = scanner.nextInt();
        }catch (Exception e){
            System.out.println("输入不合法，请重新操作。");
            while (scanner.hasNext()) {
                if (scanner.hasNextLine()) {
                    scanner.nextLine();
                    break;
                } else {
                    scanner.next();
                }
            }
            addToTrolley();
            return;
        }
        if(good.getCount()<cnt){
            System.out.println("商品不足，仅剩余"+ good.getCount()+"件，输入任意字符继续");
            scanner.next();
            return;
        }
        shoppingTrolley.put(good, cnt);
        System.out.println("加入购物车成功！输入任意字符继续。");
        scanner.next();
    }
    public void show(){
        System.out.println("客户ID:"+ID+"  用户名:"+getAccount()+"  用户级别:"+level+"  用户注册时间:"+registerTime
                +"  客户累计消费总金额:"+getCostTotal()+"  用户手机号:"+getPhoneNumber()+"  用户邮箱:"+getMailbox());
    }
}
